<?php

namespace common\models\mongo;

use Yii;
use youmbe\sports\NanoSports;
use common\helpers\ArrayHelper;
use common\models\ofc\OfcMatch;

/**
 * This is the model class for collection "match_lineup".
 *
 * @property \MongoDB\BSON\ObjectID|string $_id
 * @property mixed $id
 * @property mixed $channel_id
 * @property mixed $info
 * @property mixed $matchevent
 * @property mixed $home_team
 * @property mixed $away_team
 * @property mixed $tlive
 * @property mixed $stats
 * @property mixed $created_at
 * @property mixed $updated_at
 */
class MatchDetail extends \common\db\MongoActiveRecord {

    /**
     * @var boolean 是否强制刷新
     */
    public $forceRefresh = false;

    /**
     * {@inheritdoc}
     */
    public static function collectionName() {
        return 'match_detail';
    }

    /**
     * {@inheritdoc}
     */
    public function attributes() {
        return [
            '_id',
            'id',
            'channel_id',
            'info',
            'matchevent',
            'home_team',
            'away_team',
            'tlive',
            'stats',
            'created_at',
            'updated_at'
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function rules() {
        return [
            [['id', 'channel_id', 'info', 'matchevent', 'home_team', 'away_team', 'tlive', 'stats', 'created_at', 'updated_at'], 'safe']
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels() {
        return [
            '_id' => 'ID',
            'id' => 'Id',
            'channel_id' => 'Channel ID',
            'info' => 'info',
            'matchevent' => 'matchevent',
            'home_team' => 'home_team',
            'away_team' => 'away_team',
            'tlive' => 'tlive',
            'stats' => 'stats',
        ];
    }

    public function checkRefresh() {
        if ($this->isNewRecord) {
            return true;
        }
        $status = ArrayHelper::getValue($this->info, 'statusid');
        if ($status < OfcMatch::MATCH_STATUS_COMPLETED && $status > OfcMatch::MATCH_STATUS_WAIT && $this->updated_at + 10 < time()) {
            return true;
        }
        if ($status <= OfcMatch::MATCH_STATUS_WAIT && $this->updated_at + 60 < time()) {
            return true;
        }
        return $this->forceRefresh;
    }

    /**
     * 查询数据信息
     * @params array|mixed $condition
     * @return static
     */
    public static function findCurrent($condition, $force = false) {
        $model = static::findOrNew($condition);
        $model->forceRefresh = $force;
        if ($model->checkRefresh()) {
            $data = $model->loadChannelData();
            if (!$data) {
                return $model->isNewRecord ? null : $model;
            }
            Yii::error([
                'force' => $force,
                'date' => date('Y-m-d H:i:s'),
                $data
            ], __METHOD__);
            $model->attributes = $data;
            $model->save();
        }
        return $model;
    }

    public function loadChannelData() {
        $sports = new NanoSports();
        return $sports->match($this->channel_id);
    }

    public function fields() {
        return [
            'info',
            'matchevent',
            'home_team',
            'away_team',
            'tlive',
            'stats',
        ];
    }

}
